package com.hctforgreen.greenservice.ui.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hctforgreen.greenservice.MyFeedbackActivity;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.ShowMegaImageActivity;
import com.hctforgreen.greenservice.ctr.HctController;
import com.hctforgreen.greenservice.model.BaseEntity;
import com.hctforgreen.greenservice.model.CompetInfoListEntity.CompetInfo;
import com.hctforgreen.greenservice.model.FeedBackListEntity.FeedBackEntity;
import com.hctforgreen.greenservice.model.FeedBackMessageEntity;
import com.hctforgreen.greenservice.model.ResultEntity;
import com.hctforgreen.greenservice.ui.handler.CompetInfoListHandler;
import com.hctforgreen.greenservice.ui.page.AbstractDataLoaderHandler;
import com.hctforgreen.greenservice.ui.page.AbstractPageableAdapter;
import com.hctforgreen.greenservice.utils.AsyncImageLoader;
import com.hctforgreen.greenservice.utils.AsyncImageLoader.ImageCallback;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;
import com.hctforgreen.greenservice.utils.KeyboardUtil;
import com.hctforgreen.greenservice.utils.LoginResultStoreUtil;
import com.hctforgreen.greenservice.utils.Md5Util;
import com.hctforgreen.greenservice.utils.Utils;

public class CompetInfoListAdapter extends AbstractPageableAdapter<BaseEntity>
		implements OnItemClickListener {

	private Activity mActivity;
	private View mConvertView;
	private ListView mListView;
	private CompetInfoListHandler mHandler;

	public CompetInfoListAdapter(ListView listView, Activity activity,
			View convertView, int loadingViewResourceId,
			int reloadViewResourceId,
			AbstractDataLoaderHandler<BaseEntity> handler) {
		super(listView, activity, loadingViewResourceId, reloadViewResourceId,
				handler);
		mListView = listView;
		mActivity = activity;
		mConvertView = convertView;

		mHandler = (CompetInfoListHandler) handler;
		mListView.setAdapter(this);
		mListView.setOnItemClickListener(this);
		mListView.setOnScrollListener(this);
		mListView.setDivider(mActivity.getResources().getDrawable(
				R.drawable.ic_driver));
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewCache viewCache;
		final CompetInfo entity = (CompetInfo) getItem(position);
		if (convertView == null) {
			convertView = View.inflate(mActivity,
					R.layout.item_compet_info_lst, null);
			viewCache = new ViewCache(convertView);
			convertView.setTag(viewCache);
		} else {
			viewCache = (ViewCache) convertView.getTag();
		}

		viewCache.getSeriesNameTv().setText(entity.brand);
		viewCache.getSubmitDateTv().setText(entity.submitDate);
		viewCache.getContentTv().setText(
				mActivity.getString(R.string.feedback_adapter_content_tag)
						+ entity.content);
		viewCache.getTitleTv().setText(
				mActivity.getString(R.string.feedback_adapter_theme_tag)
						+ entity.topic);

		renderPicLayout(viewCache, entity);
		if (TextUtils.isEmpty(entity.videoPath)) {
			viewCache.getTvVideo().setVisibility(View.GONE);
		} else {
			viewCache.getTvVideo().setVisibility(View.VISIBLE);
		}
		viewCache.getTvVideo().setText("    播放视频");
		viewCache.getTvVideo().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					Intent intent = new Intent(Intent.ACTION_VIEW);
					Uri uri = Uri.parse(entity.videoPath);
					intent.setDataAndType(uri, "video/*");
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					mActivity.startActivity(intent);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		if (TextUtils.isEmpty(entity.attachmentPath)) {
			viewCache.getTvAttach().setVisibility(View.GONE);
		} else {
			viewCache.getTvAttach().setVisibility(View.VISIBLE);
		}
		viewCache.getTvAttach().setText("    查看资料");
		viewCache.getTvAttach().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.setData(Uri.parse(entity.attachmentPath));
					mActivity.startActivity(intent);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		return convertView;
	}

//	private void renderPicLayout(LinearLayout picLayout,
//			FeedBackMessageEntity entity) {
//
//		// if (viewCache.getPicParentTv().getChildCount() == 0) {
//		// for (int i = 0; i < 6; i++) {
//		FrameLayout choiceLyt = (FrameLayout) mActivity.getLayoutInflater()
//				.inflate(R.layout.layout_pic, null);
//		choiceLyt.setId(1);
//		if (picLayout.getChildCount() == 0) {
//			picLayout.addView(choiceLyt, 0);
//		}
//		// }
//		// }
//		//
//		// int iFreeSize = 6 - entity.picNames.size();
//		// if (iFreeSize > 0) {
//		// for (int i = 5; i > entity.picNames.size() - 1; i--) {
//		// FrameLayout choiceLyt = (FrameLayout) viewCache
//		// .getPicParentTv().findViewById(i);
//		//
//		// choiceLyt.setVisibility(View.GONE);
//		//
//		// }
//		// // }
//		// FrameLayout choiceLyt = (FrameLayout) mActivity.getLayoutInflater()
//		// .inflate(R.layout.layout_pic, null);
//		choiceLyt.setVisibility(View.VISIBLE);
//		// viewCache.getPicParentTv().addView(choiceLyt);
//		renderPicIv(entity.messagePicPath, choiceLyt);
//		// viewCache.getPicParentTv().addView(choiceLyt);
//
//	}

	/**
	 * 2016 12 8 390778
	 * 暂时解决android studio打包问题
	 * choiceLyt.setId(i+0);
	 * 动态添加控件时需要设置id，
	 * 在当前的activity界面上id要保证唯一
	 * （i+0）修改不严谨，需要进行优化
	 * @param viewCache
	 * @param entity
	 */

	private void renderPicLayout(final ViewCache viewCache,
			final CompetInfo entity) {

		if (viewCache.getPicParentTv().getChildCount() == 0) {
			for (int i = 0; i < 6; i++) {
				FrameLayout choiceLyt = (FrameLayout) mActivity
						.getLayoutInflater().inflate(R.layout.layout_pic, null);
				choiceLyt.setId(i+0);
				viewCache.getPicParentTv().addView(choiceLyt, i);
			}
		}

		int iFreeSize = 6 - entity.picNames.size();
		if (iFreeSize > 0) {
			for (int i = 5; i > entity.picNames.size() - 1; i--) {
				FrameLayout choiceLyt = (FrameLayout) viewCache
						.getPicParentTv().findViewById(i+0);

				choiceLyt.setVisibility(View.GONE);
			}
		}
		for (int i = 0; i < entity.picNames.size(); i++) {
			if (!"".equals(entity.picNames.get(i).trim())) {
				String sPicName = buildPicUrl(entity, i);
				FrameLayout choiceLyt = (FrameLayout) viewCache
						.getPicParentTv().findViewById(i+0);
				choiceLyt.setVisibility(View.VISIBLE);
				renderPicIv(sPicName, choiceLyt);
			}
		}

	}

	private String buildPicUrl(final CompetInfo entity, int i) {
		return HctConstants.NET_URL
				+ "vieinfo-mobile!getVieinfoPicByName.do?id=" + entity.id
				+ "&picName=" + entity.picNames.get(i);
	}

	private void renderPicIv(final String sPicName, final FrameLayout choiceLyt) {
		final ImageView choiceIv = (ImageView) choiceLyt
				.findViewById(R.id.iv_choice);
		choiceIv.setTag(Md5Util.md5(sPicName));
		AsyncImageLoader imageLoader = new AsyncImageLoader();
		Drawable cacheDrawable = imageLoader.loadDrawable(sPicName, mActivity,
				true, new ImageCallback() {

					@Override
					public void imageLoaded(Drawable imageDrawable,
							String imageUrl) {
						ImageView tagImage = (ImageView) choiceLyt
								.findViewWithTag(imageUrl);
						if (tagImage != null) {
							if (imageDrawable != null) {
								tagImage.setImageDrawable(imageDrawable);
							} else {
								tagImage.setImageResource(R.drawable.ic_default_pic);
							}
						}

					}
				});
		if (cacheDrawable != null) {
			choiceIv.setImageDrawable(cacheDrawable);
		} else {
			choiceIv.setImageResource(R.drawable.ic_default_pic);
		}
		choiceIv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.putExtra("image", sPicName);
				intent.setClass(mActivity, ShowMegaImageActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				mActivity.startActivity(intent);
				// Toast.makeText(mActivity, sPicName,
				// Toast.LENGTH_SHORT).show();
			}
		});
	}

	class ViewCache {
		private View baseView;
		private TextView submitDate;
		private TextView title;
		private TextView content;
		private TextView seriesName;

		private TextView tvVideo;// 视频
		private TextView tvAttach;// 附件

		private LinearLayout picParent;
		private LinearLayout subListView;

		private View msgView;

		private View hideOrShowImgs;

		private ImageView imgState;

		public ViewCache(View view) {
			baseView = view;
		}

		public View getMsgView() {

			if (msgView == null) {
				msgView = baseView.findViewById(R.id.layout_message);
			}
			return msgView;
		}

		public View getHideOrShowImgs() {
			if (hideOrShowImgs == null) {
				hideOrShowImgs = baseView.findViewById(R.id.hide_show_layout);
			}

			return hideOrShowImgs;
		}

		public ImageView getImgState() {
			if (imgState == null) {
				imgState = (ImageView) baseView
						.findViewById(R.id.img_hide_show);
			}
			return imgState;
		}

		public LinearLayout getSubListView() {
			if (subListView == null) {
				subListView = (LinearLayout) baseView
						.findViewById(R.id.msg_list);
			}

			return subListView;
		}

		public TextView getTvAttach() {
			if (tvAttach == null) {
				tvAttach = (TextView) baseView.findViewById(R.id.tv_attach);
			}
			return tvAttach;
		}

		public TextView getTvVideo() {
			if (tvVideo == null) {
				tvVideo = (TextView) baseView.findViewById(R.id.tv_video);
			}
			return tvVideo;
		}

		public TextView getSubmitDateTv() {
			if (submitDate == null) {
				submitDate = (TextView) baseView
						.findViewById(R.id.tv_submit_date);
			}
			return submitDate;
		}

		public TextView getTitleTv() {
			if (title == null) {
				title = (TextView) baseView.findViewById(R.id.tv_title);
			}
			return title;
		}

		public TextView getContentTv() {
			if (content == null) {
				content = (TextView) baseView.findViewById(R.id.tv_content);
			}
			return content;
		}

		public TextView getSeriesNameTv() {
			if (seriesName == null) {
				seriesName = (TextView) baseView
						.findViewById(R.id.tv_series_name);
			}
			return seriesName;
		}

		public LinearLayout getPicParentTv() {
			if (picParent == null) {
				picParent = (LinearLayout) baseView
						.findViewById(R.id.lyt_pic_parent);
			}
			return picParent;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
	}

}
